/*****************************************************************************/
/**
*
* @file bsp_iic.c
*
* User defined functions about iic of stm32.
* Target device: STM32F103RET6.
*
* <pre>
* MODIFICATION HISTORY:
*
* Ver   Who    Date   	Changes
* ----- ---- ---------- -------------------------------------------------------
* 0.01  abu  02/17/2020 Created
* </pre>
*
*------------------------------------------------------------------------------
* Author:	abu
*					E-mail:	abu_liu@opencores.org
*------------------------------------------------------------------------------
*
******************************************************************************/

/***************************** Include Files *********************************/
#include "bsp_iic.h"

/************************** Function Prototypes ******************************/

/*****************************************************************************/
/**
*
* @brief	Initialise IIC2.
*
* @param	None.
* @param  None.	
*
* @return	None.
*
* @note		None.
*
******************************************************************************/
void I2C2_Init(void)
{
	GPIO_InitTypeDef I2C2_GPIO_InitStruct;
	I2C_InitTypeDef I2C2_I2C_InitStruct;
	
	//open gpio port clock and iic clock
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);

	//====================gpio related========================
	//SCL, PB10
	I2C2_GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
	I2C2_GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	I2C2_GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;	//alternate function open-drain output
	GPIO_Init(GPIOB, &I2C2_GPIO_InitStruct);
	
	//SDA, PB11
	I2C2_GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11;
	I2C2_GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	I2C2_GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;	//alternate function open-drain output
	GPIO_Init(GPIOB, &I2C2_GPIO_InitStruct);
	
	//====================IIC related========================
	I2C2_I2C_InitStruct.I2C_ClockSpeed = 400000;	//iic clock 400khz
	I2C2_I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;	//iic mode
	I2C2_I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
	I2C2_I2C_InitStruct.I2C_OwnAddress1 = 0x0a;	//master device iic address 0x0a
	I2C2_I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;	//enable ack
	I2C2_I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;	//7 bit slave address
	I2C_Init(I2C2, &I2C2_I2C_InitStruct);
	I2C_Cmd(I2C2, ENABLE);
}
